<?php
function p($data) {
    echo '<pre>';
    print_r($data);
}

/**
 * Created by PhpStorm.
 * User: CPR007
 * Date: 2018/8/9
 * Time: 15:10
 */
class coreBase {
    public static $logId;
    public $logMode = [
        1 => 'info',
        2 => 'error',
        3 => 'suc',
        4 => 'sql'
    ];
    protected static $dbInstance;
    protected static $hashDbInstance;
    protected static $redisInstance;
    protected $_configInfo;
    private $_configFile = '/env.ini';
    private $_logDir = '/log/';

    public function __construct() {
        if (!file_exists(__DIR__ . $this->_configFile)) {
            echo '缺少配置文件';
            exit;
        }
        if (!is_dir(__DIR__ . $this->_logDir)) {
            mkdir(__DIR__ . $this->_logDir, 0777, true);
            chmod(__DIR__ . $this->_logDir, 0777);
        }
        $this->_configInfo = parse_ini_file(__DIR__ . $this->_configFile, true);
    }


    /**
     * @param $config
     */
    public function _getDbInstance($config) {
        $dsn = 'mysql:host=' . $config['host'] . ';port=' . $config['port'] . ';dbname=' . $config['db_name'];
        self::$dbInstance = new \PDO($dsn, $config['user'], $config['pwd']);
        try {
            if (!isset(self::$dbInstance)) {
                $dsn = 'mysql:host=' . $config['host'] . ';port=' . $config['port'] . ';dbname=' . $config['db_name'];
                self::$dbInstance = new \PDO($dsn, $config['user'], $config['pwd']);
                self::$dbInstance->exec("SET NAMES UTF8");//设置数据库编码
                self::$dbInstance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);//设置警告模式
            }
        } catch (\Throwable $e) {
            $this->writeLog($e->getMessage(), 2);
            return;
        }
    }

    /**
     * @param $config
     * @return bool
     * 获取redis链接
     */
    public function getRedisInstance($config) {
        try {
            if (!self::$redisInstance) {
                self::$redisInstance = new Redis();
                self::$redisInstance->connect($config['host'], $config['port'], $config['timeout']??3);
                if (isset($config['auth'])) self::$redisInstance->auth($config['auth']);
                if (isset($config['db'])) self::$redisInstance->select($config['db']);
            }
        } catch (\Throwable $e) {
            $this->writeLog(__FILE__ . "文件第" . __LINE__ . "行，" . $e->getMessage(), 2);
            return false;
        }
    }


    /**
     * @return string
     * 获取logId
     */
    public function getLogId() {
        if (self::$logId === NULL) {
            self::$logId = uniqid();
        }
        return self::$logId;
    }

    /**
     * @param $msg
     * @param $mode
     * 记录日志文件
     */
    public function writeLog($msg, $mode) {
        $tag = isset($this->logMode[$mode]) ? $this->logMode[$mode] : $mode;
        $logFile = __DIR__ . $this->_logDir . date("Y_m_d") . '_' . $tag . '.log';
        $logId = $this->getLogId();
        $date = date("H:i:s");
        $msg = "[$date][logId: $logId]: $msg" . PHP_EOL;
        file_put_contents($logFile, $msg, FILE_APPEND);
    }
}